import {createApp} from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import Vuex from 'vuex'

const app = createApp(App)
app.use(ElementPlus)
app.use(store).use(Vuex).use(router).use(VueMarkdownEditor).mount('#app');

import * as ElIcon from '@element-plus/icons-vue'

for (let iconName in ElIcon){
    app.component(iconName, ElIcon[iconName])
}

import VueMarkdownEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import vuepressTheme from '@kangc/v-md-editor/lib/theme/vuepress.js';
import '@kangc/v-md-editor/lib/theme/style/vuepress.css';
import createLineNumberPlugin from '@kangc/v-md-editor/lib/plugins/line-number/index';
import createCopyCodePlugin from '@kangc/v-md-editor/lib/plugins/copy-code/index';
import '@kangc/v-md-editor/lib/plugins/copy-code/copy-code.css';
import createHighlightLinesPlugin from '@kangc/v-md-editor/lib/plugins/highlight-lines/index';
import '@kangc/v-md-editor/lib/plugins/highlight-lines/highlight-lines.css';

import Prism from 'prismjs'

VueMarkdownEditor.use(vuepressTheme, {
    Prism,
}, createLineNumberPlugin(), createCopyCodePlugin(), createHighlightLinesPlugin());



import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";


// 插件形式使用请求
createApp(App).prototype.postRequest = postRequest
createApp(App).prototype.putRequest = putRequest
createApp(App).prototype.getRequest = getRequest
createApp(App).prototype.deleteRequest = deleteRequest

router.beforeEach((to, from, next) => {
    if (window.sessionStorage.getItem("tokenStr")) {
        if (!window.sessionStorage.getItem('user')) {
            return getRequest('/login/info',).then(resp => {
                if (resp) {
                    window.sessionStorage.setItem('user', JSON.stringify(resp));
                }
                next("/home");
            })
        }
        next();
    } else {
        if (to.path == '/') {
            next();
        } else {
            next('/?redirect=' + to.path);
        }
    }
})



